package com.habdqn.filght.information.dao.impl;

import com.habdqn.filght.information.dao.BaseDao;
import com.habdqn.filght.information.dao.FlightTicketDao;
import com.habdqn.filght.information.entity.FlightTicket;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Xing.Jiejian
 * @date 2020/11/24
 */
public class FlightTicketDaoImpl extends BaseDao implements FlightTicketDao {
    @Override
    public List<FlightTicket> findByFlightId(int flightId) {
        String sql = "SELECT id, flight_id, ticket_price, sell_company FROM flight_ticket WHERE flight_id=?";
        try{
            ResultSet rs = executeQuery(sql,flightId);
            List<FlightTicket> flightTickets = new ArrayList<>();
            while (rs.next()){
                FlightTicket flightTicket = new FlightTicket();
                flightTicket.setId(rs.getInt("id"));
                flightTicket.setFlightId(flightId);
                flightTicket.setTicketPrice(rs.getFloat("ticket_price"));
                flightTicket.setSellCompany(rs.getString("sell_company"));
                flightTickets.add(flightTicket);
            }
            return flightTickets;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return null;
    }
}
